
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;


using Yozeev.BusinessLogic;
using Yozeev.BusinessLogic.Dtos;
using Yozeev.BusinessLogic.DomainService;
using Yozeev.BusinessLogic.Authorization;
using Yozeev.SystemConfig;
using Yozeev.Common;
using Abp.Authorization.Users;

namespace Yozeev.BusinessLogic
{
	/// <summary>
	/// AccountDetail应用层服务的接口实现方法  
	///</summary>
	[AbpAuthorize]
	public class AccountDetailAppService : YozeevAppServiceBase, IAccountDetailAppService
    {
        private readonly IRepository<AccountDetail, Guid> _entityRepository;

		private readonly IRepository<Account, Guid> _accountRepository;

		private readonly IRepository<PayMethod, Guid> _payMethodRepository;

		private readonly IAccountDetailManager _entityManager;

		private readonly IQueryHelper _queryHelper;

		/// <summary>
		/// 构造函数 
		///</summary>
		public AccountDetailAppService(
        IRepository<AccountDetail, Guid> entityRepository
        ,IAccountDetailManager entityManager
		,IRepository<Account, Guid> accountRepository
		,IRepository<PayMethod, Guid> payMethodRepository
			,IQueryHelper queryHelper
        )
        {
            _entityRepository = entityRepository; 
             _entityManager=entityManager;
			_accountRepository = accountRepository;
			_payMethodRepository = payMethodRepository;
			_queryHelper = queryHelper;
        }


		/// <summary>
		/// 获取AccountDetail的分页列表信息
		///</summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<PagedResultDto<AccountDetailListDto>> GetPaged(GetAccountDetailsInput input)
		{

			var expression = _queryHelper.WhereExp<AccountDetail>(input.queryData);
			if (AbpUserBase.AdminUserName != GetCurrentUser().UserName)
				expression = _queryHelper.SetWhereExp<AccountDetail>(expression, GetViewTypeEnum<AccountDetail>(), await GetBranchUser());
			var query = from accountdetail in _entityRepository.GetAll().AsNoTracking()
						 .Where(expression)
						select new
						{
							accountdetail,
							accountdetail.Account,
                            accountdetail.ActivityDetail,
                            accountdetail.CreatorUser,
                            accountdetail.ActivityDetail.Activity,
                            accountdetail.ActivityDetail.TicketPrice.Ticket,
                            accountdetail.ActivityDetail.TicketPrice,
                        };
			var list = new List<AccountDetail>();
			query.ToList().ForEach(temp => {
				var _accountdetail = temp.accountdetail;
				_accountdetail.Account = temp.Account;
                _accountdetail.ActivityDetail = temp.ActivityDetail;
                _accountdetail.CreatorUser = temp.CreatorUser;
				list.Add(_accountdetail);
			});
			// TODO:根据传入的参数添加过滤条件
			var count = list.Count();
			list = list.AsQueryable().OrderBy(input.Sorting)
					.PageBy(input)
					.ToList();
			var entityList = list;
			var entityListDtos =entityList.MapTo<List<AccountDetailListDto>>();

			return new PagedResultDto<AccountDetailListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取AccountDetailListDto信息
		/// </summary>
		public async Task<AccountDetailListDto> GetById(EntityDto<Guid> input)
		{
			var entity = await _entityRepository.GetAsync(input.Id);

		    return entity.MapTo<AccountDetailListDto>();
		}

		/// <summary>
		/// 获取编辑 AccountDetail
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task<GetAccountDetailForEditOutput> GetForEdit(NullableIdDto<Guid> input)
		{
			var output = new GetAccountDetailForEditOutput();
AccountDetailEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _entityRepository.GetAsync(input.Id.Value);

				editDto = entity.MapTo<AccountDetailEditDto>();

				//accountDetailEditDto = ObjectMapper.Map<List<accountDetailEditDto>>(entity);
			}
			else
			{
				editDto = new AccountDetailEditDto();
			}

			output.AccountDetail = editDto;
			return output;
		}


		/// <summary>
		/// 添加或者修改AccountDetail的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task CreateOrUpdate(CreateOrUpdateAccountDetailInput input)
		{

			if (input.AccountDetail.Id.HasValue)
			{
				await Update(input.AccountDetail);
			}
			else
			{
				await Create(input.AccountDetail);
			}
		}


		/// <summary>
		/// 新增AccountDetail
		/// </summary>
		protected virtual async Task<AccountDetailEditDto> Create(AccountDetailEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <AccountDetail>(input);
            var entity=input.MapTo<AccountDetail>();
			

			entity = await _entityRepository.InsertAsync(entity);
			return entity.MapTo<AccountDetailEditDto>();
		}

		/// <summary>
		/// 编辑AccountDetail
		/// </summary>
		protected virtual async Task Update(AccountDetailEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);

			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}



		/// <summary>
		/// 删除AccountDetail信息的方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		public async Task Delete(EntityDto<Guid> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除AccountDetail的方法
		/// </summary>
		public async Task BatchDelete(List<Guid> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出AccountDetail为excel表,等待开发。
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


